﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace ExcelUtli
{
     class IniOpr
    {
        public string iniPath = "";
        public IniOpr(string path)
        {
            this.iniPath = path;
        }

        public void IniWriteValue(string Section, string Key, string Value)//写
        {
            WritePrivateProfileString(Section, Key, Value, this.iniPath);
        }
        public string IniReadValue(string Section, string Key) //读
        {
            StringBuilder temp = new StringBuilder(256);
            int i = GetPrivateProfileString(Section, Key, "", temp, 256, this.iniPath);
            return temp.ToString();
        }
        public static string IniAppPath()//获取路径
        {
            return System.Windows.Forms.Application.StartupPath + "\\";
        }
        #region API函数
        /// 写INI文件
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">键</param>
        /// <param name="iValue">值</param>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        /// 读取INI文件      
        /// </summary>     
        /// <param name="section">节点名称</param>  
        /// <param name="key">键</param>   
        /// <param name="def">值</param> 
        /// <param name="retval">stringbulider对象</param>  
        /// <param name="size">字节大小</param>      
        /// <param name="filePath">文件路径</param>   
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
        #endregion
    }
}
